package com.warf.warftool.was;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ibm.ws.scripting.WasxShell;
import com.ibm.wsspi.ssl.RetrieveSignersHelper;
import com.warf.warftool.utils.WarfUtils;

public class WasConfigApp implements Runnable{
	String dmgrHostName= "localhost";
	String dmgrPortName="8082";
	String dmgrUsername;
	String dmgrPassword;
	String soapCfgURL;
    String sslCfgURL;
    String configScript="createApp.py";
    private String rootFolder;
    private String webappsDir="";
    private String scriptPath;
    String parm;
    String startTime="";
    String endTime= "";
    String appName="";
    
    boolean sendemail=false;
    boolean enablebackup=false;
    String  emailTo="";
    String emailFrom="WarfTool@No-Reply.com";
    String  smtphost="";
    int exitcode=1;
    
    String configAction="";
    String configTask="";
    
    public WasConfigApp(String name){
    	appName=name;
    	soapCfgURL = "soap.client.props.ibm";
	    sslCfgURL = "ssl.client.props.ibm";
	    rootFolder = "/";
	    scriptPath=rootFolder;
	    
	    
    }
    
	public void runScript(){
		WasxShell shell = new WasxShell();
		RetrieveSignersHelper.getInstance().autoAcceptSignerAndStoreInTrustStore();
	
		 if(rootFolder != null){
			 soapCfgURL = (new StringBuilder("file:")).append(rootFolder).append("/etc/").append(soapCfgURL).toString();
	         sslCfgURL = (new StringBuilder("file:")).append(rootFolder).append("/etc/").append(sslCfgURL).toString();
	         System.setProperty("was.install.root", rootFolder);
	         System.setProperty("user.root", (new StringBuilder(String.valueOf(rootFolder))).append("/etc/").toString());
	         
		 }
		 System.setProperty("com.ibm.SOAP.ConfigURL", soapCfgURL);
	     System.setProperty("com.ibm.SSL.ConfigURL", sslCfgURL);
	     System.setProperty("SCRIPTPATH", scriptPath);	 
	     System.setProperty("WEBAPPSDIR", webappsDir);
	     System.setProperty("org.apache.soap.util.net.SSLUtils.buildSSLSocket.timeout", "6000000");
	     try
	        {
	    	   List<String> argslist=new ArrayList<String>();
	    	   argslist.add("-connType");
	    	   argslist.add("SOAP");
	    	   argslist.add("-host");
	    	   argslist.add(dmgrHostName);
	    	   argslist.add("-port");
	    	   argslist.add(dmgrPortName);
	    	   argslist.add("-lang");
	    	   argslist.add("jython");
	    	   argslist.add("-user");
	    	   argslist.add(dmgrUsername);
	    	   argslist.add("-password");
	    	   argslist.add(dmgrPassword);
	    	   argslist.add("-f");
	    	   argslist.add(scriptPath+"/jython/config/"+configScript);
	    	   
	    	   if(!configAction.equalsIgnoreCase("")){
	    		   argslist.add("-a");
	    		   argslist.add(configAction);
	    	   }
	    	   if(!configTask.equalsIgnoreCase("")){
	    		   argslist.add("-t");
	    		   argslist.add(configTask);
	    	   }
	    	   argslist.add(appName);  
	    	   System.out.println((new StringBuilder("*********************")).append(shell.getConnectionType()).toString());
	           String[] ol = argslist.toArray(new String[argslist.size()]);
	           startTime=new Date().toString();
	           System.out.println("*********************************************START TIME: "+startTime);
	           shell.run(ol);
	           exitcode=shell.getReturnCode();
	           endTime=new Date().toString();
//	           if(sendemail){
//	        	   sendEmail();
//	           }
	           System.out.println("**********************************************END TIME: "+endTime+ " Exit code: "+exitcode);
	           
	    	   
	        }
	     catch(Exception e)
	        {
	            e.printStackTrace();
	        }
	}
	
	public String getDmgrHostName() {
		return dmgrHostName;
	}

	public void setDmgrHostName(String dmgrHostName) {
		this.dmgrHostName = dmgrHostName;
	}

	public String getDmgrPortName() {
		return dmgrPortName;
	}

	public void setDmgrPortName(String dmgrPortName) {
		this.dmgrPortName = dmgrPortName;
	}

	public String getDmgrUsername() {
		return dmgrUsername;
	}

	public void setDmgrUsername(String dmgrUsername) {
		this.dmgrUsername = dmgrUsername;
	}

	public String getDmgrPassword() {
		return dmgrPassword;
	}

	public void setDmgrPassword(String dmgrPassword) {
		this.dmgrPassword = dmgrPassword;
		
		if(dmgrPassword.startsWith("{xor}")){
			this.dmgrPassword=WarfUtils.decryptWasPassword(dmgrPassword);
		}else{
			dmgrPassword=dmgrPassword;
		}
	}
	

	public String getScriptPath() {
		return scriptPath;
	}

	public void setScriptPath(String scriptPath) {
		this.scriptPath = scriptPath;
	}

	public String getRootFolder() {
		return rootFolder;
	}

	public void setRootFolder(String rootFolder) {
		this.rootFolder = rootFolder;
	}

	public String getWebappsDir() {
		return webappsDir;
	}

	public void setWebappsDir(String webappsDir) {
		this.webappsDir = webappsDir;
	}

	public void sendEmail(){
		
	}
	
 
	public int getExitcode() {
		return exitcode;
	}

	public void setExitcode(int exitcode) {
		this.exitcode = exitcode;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		runScript();
	}

}
	